package ${packageName}.${moduleName}.dao;

import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Arrays;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ${packageName}.${moduleName}.cache.${className}Cache;
import ${packageName}.${moduleName}.model.${className};
import ${mainPath}.core.db.AbstractDbService;
import ${mainPath}.core.db.BaseEntityDao;
import ${mainPath}.util.bean.Page;

/**
 * @ClassName: ${className}Dao.java
 * @Description: ${description}的数据交互处理类
 * @author ${author}
 * @date   ${newDate}
 * @version V1.0.0
 * <p>
 * ----------------------------------------------------------------------------
 *  modifyer    modifyTime                 comment
 *
 * ----------------------------------------------------------------------------
 * </p>
 */
@Component
public class ${className}Dao extends BaseEntityDao<${className}> {

	private Logger logger = LoggerFactory.getLogger(${className}Dao.class);

	@Autowired
	private ${dataSource} ${dataSourceLower};

	@Autowired
	private ${className}Cache ${paramName}Cache;

	private final String SQL = "SELECT  ${selectSQL} ";

	private final String SQL_INSERT = "INSERT INTO ${tableName} (${insertSQL}) "
				+ " VALUES(${insertVal}) ";
	private final String SQL_INSERT_ID = "INSERT INTO ${tableName} (${insertSQL},${columnId}) "
				+ " VALUES(${insertVal} ,?) ";

	private final String SQL_UPDATE = "UPDATE ${tableName} SET ${updateSQL} "
				+ " WHERE ${columnId} = ? " ;


	private final String SQL_DELETE = " DELETE FROM ${deleteSQL}  = ? ";

	private final String SQL_DELETE_IN = " DELETE FROM ${deleteSQL} IN ( ? ) ";

	protected AbstractDbService dataSource(){
		return dataSource;
	}

	/**
	 * 保存创建${className},并且保存到缓存中
	 * @param ${paramName}
	 */
      public ${className} save${className}(${className} ${paramName}){
        if(null == ${paramName})
            return  null;

		${insertDbList}

       ${paramName} =  this.saveEntity(${paramName},SQL_INSERT,${dbList});
        return ${paramName};

      }


	/**
	* 保存创建${className},并且保存到缓存中
	* @param ${paramName}
	*/
	public boolean saveId${className}(${className} ${paramName}){
		if(null == ${paramName})
			return  false;

		${insertDbList}
		${updateWhereDbList}
        return this.saveIdEntity(${paramName},SQL_INSERT_ID,${dbList});

	}


	/**
	 * 更新${paramName}信息,where条件为${paramName}Id,更新${paramName}
	 * @param ${paramName}
	 */
	public boolean update${className}(${className} ${paramName}){
		boolean flg = false ;
		if(null == ${paramName})
		   return  flg;

		${updateDbList}
		${updateWhereDbList}
		flg = this.updateEntity(${paramName},SQL_UPDATE,${dbList});
		return flg;

	}

	/**
	 * 通过主键${paramName}Id删除${paramName}
	 *  @param ${paramName}Id
	 */
	public boolean  del${className}ById(long  ${paramName}Id){
		if(${paramName}Id == 0)
	        return false;
		return  this.delEntityById(${className}.class,SQL_DELETE,${paramName}Id);

	}

	/**
	* 通过主键${paramName}Id删除${paramName}
	*  @param ${paramName}Ids
	*/
	public int  del${className}ByIdList(List<Long>  ${paramName}Ids){
		if(${paramName}Ids == null || ${paramName}Ids.isEmpty() )
			return 0;
		return  this.delEntityByIdList(${className}.class,SQL_DELETE_IN,${paramName}Ids);

	}




	/**
	 * 通过主键${paramName}删除${paramName}
	 *  @param ${paramName}
	 */
	public boolean  del${className}(${className} ${paramName}){
		if(null == ${paramName} || ${id} < 0)
	        return false;
        return  this.delEntityById(${className}.class,SQL_DELETE,${paramName}.getId());

	}

		/**
	 * 通过主键${paramName}Id查找${className}
	 * @param ${paramName}
	 */
	public ${className} get${className}(${className} ${paramName}){
		if(null == ${paramName} || ${id} < 0){
		   return  null;
		}
		long id =  ${id};
        String sql = SQL
               + " FROM ${tableName}  WHERE ${columnId} = ? ";
        return this.getEntityById(${className}.class,${paramName}Mapper,sql,id);
	}

	/**
	 * 通过主键${paramName}Id查找${className}
	 * @param ${paramName}Id
	 */
	public ${className} get${className}ById(long ${paramName}Id){
		if(${paramName}Id <= 0)
		   return  null;

		String sql = SQL
		       + " FROM ${tableName}  WHERE ${columnId} = ? ";

        return this.getEntityById(${className}.class,${paramName}Mapper,sql,${paramName}Id);


	}


	/**
	 * 通过主键id集合批量查找${className} Collection<Long>
	 * @param colle
	 */
	public List<${className}>  get${className}ListByIdList(Collection<Long> colle){
		if(null == colle || colle.isEmpty()) {
		   return  null;
		}

		String sql = SQL
               + " FROM ${tableName}  WHERE ${columnId} IN ( ? ) ";

        List<${className}> list = this.getEntityListByIdList(${className}.class,${paramName}Mapper,sql,colle);
        return  list;
	}

	/**
	 * 通过主键id集合批量查找${className}
	 * @param colle
	 */
	public Map<Long,${className}>  get${className}MapByIdList(Collection<Long> colle){
		if(CollectionUtils.isEmpty(colle)){
			return new HashMap<Long, ${className}>();
		}

		String sql = SQL
				+ " FROM ${tableName}  WHERE ${columnId} in ( ? )  ";
        Map<Long,${className}> map = this.getEntityMapByIdList(${className}.class,${paramName}Mapper,sql,colle);
		return map;
	}
	/**
	 * 通过分页Pager查找${className}
	 * @param page
	 */
	//@Deprecated
	public List<${className}>  get${className}ListByPage(Page page){
		if(null == page ) {
		   return  null;
		}
		List<Object> list = Arrays.asList(page.getStart(),page.getPageSize());
		//需要自己实现相关的分页和缓存业务
		String sql = SQL
		        + " FROM ${tableName}  WHERE 1 =1 ORDER BY id DESC  limit ? , ?  ";

        List<${className}> dbList  = this.getEntityByPage(${className}.class,${paramName}Mapper,sql,list);//从缓存中查找

        return  dbList;

	}



	/**
	 * 查询使用映射对象Mapper
	 * @param ${paramName}Mapper
	 */
	private RowMapper<${className}> ${paramName}Mapper =(rs, rowNum) -> {
            ${className} vo = new ${className}();${resultVo}
            return vo;
	};

	@Override
	public void setSetterValues(int index, PreparedStatement ps,${className} ${paramName}) {

	}

}
